<template>
    <input type="button" value="批量上架" @click="up()">
    <input type="button" value="批量删除" @click="del()">
    <table border="1">
        <thead>
            <tr>
                <td>
                    <input type="checkbox" v-model="chead" @change="quan">
                </td>
                <td>房型名称</td>
                <td>房间号码</td>
                <td>房号描述</td>
                <td>状态</td>
            </tr>
        </thead>
        <tbody>
            <tr v-for="item in info">
                <td>
                    <input type="checkbox" :value="item.hnid" v-model="arr" :checked="cbody" >
                </td>
                <td>{{item.htname}}</td>
                <td>{{item.number}}</td>
                <td>{{item.hnprescribe}}</td>
                <td>{{item.hnstate==true?"上架":"下架"}}</td>
            </tr>
        </tbody>
    </table>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import { onMounted } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
onMounted(()=>{
    show();
})
const arr=ref([]);
const quan=()=>{
    if(chead.value==true){
        cbody.value=true;
        arr.value=info.value.map((x:any)=>{
            return x.hnid;
        })
    }
    else{
        cbody.value=false;
    }
}
const up=()=>{
    if(arr.value.length==0){
        alert('还没有选择');
        return;
    }
    else{
        if(confirm('Are you sure?')){
            axios.post('http://localhost:46564/api/House/UpNum?ids='+arr.value.toString())
            .then(res=>{
                if(res.data>0){
                    alert('上架成功');
                    show();
                }
                else{
                    alert('上架失败');
                }
            })
            .catch(error=>{
                console.log(error);
            })
        }
    }
}
const chead=ref(true);
const cbody=ref(true);


const del=()=>{
    if(arr.value.length==0){
        alert('还没有选择');
        return;
    }
    else{
        if(confirm('Are you sure?')){
            axios.get('http://localhost:46564/api/House/DelNum?ids='+arr.value.toString())
            .then(res=>{
                if(res.data>0){
                    alert('删除成功');
                    show();
                }
                else{
                    alert('删除失败');
                }
            })
            .catch(error=>{
                console.log(error);
            })
        }
    }
}
const ror=useRouter();
const info=ref([{
    "hnid": 0,
    "htid": 0,
    "number": "",
    "hnprescribe": "",
    "hnstate": true,
    "nisdel": 0,
    "htname": "",
    "htprice": "",
    "htsize": "",
    "peonum": "",
    "picture": "",
    "prescribe": "",
    "windows": false,
    "tisdel": 0
}])
const show=()=>{
    axios.get('http://localhost:46564/api/House/ShowNum')
    .then(res=>{
        info.value=res.data
    })
    .catch(error=>{
        console.log(error);
    })
}
</script>